package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qtummatrix.entity.CustomerInfoVO;
import com.qtummatrix.entity.CustomerinfoEntity;
import com.qtummatrix.service.CustomerService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("customerAction")
public class CustomerController extends ActionSupport implements ModelDriven<CustomerinfoEntity> {

	private CustomerinfoEntity customerinfoEntity = new CustomerinfoEntity();

	@Autowired
	private CustomerService customerService;

	Map<String, Object> info = new HashMap<String, Object>();

	private int delId;

	public CustomerinfoEntity getCustomerinfoEntity() {
		return customerinfoEntity;
	}

	public void setCustomerinfoEntity(CustomerinfoEntity customerinfoEntity) {
		this.customerinfoEntity = customerinfoEntity;
	}

	public int getDelId() {
		return delId;
	}

	public void setDelId(int delId) {
		this.delId = delId;
	}

	public Map<String, Object> getInfo() {
		return info;
	}

	public void setInfo(Map<String, Object> info) {
		this.info = info;
	}

	@Override
	public CustomerinfoEntity getModel() {
		// TODO Auto-generated method stub
		return customerinfoEntity;
	}

//	public String show() {
//		List<CustomerinfoEntity> list=customerService.getList();
//		info.put("list", list);
//		
//		return SUCCESS;
//	}

	public String delete() {
		int id = delId;
		customerService.delList(id);
		return SUCCESS;
	}

	public String add() {
		String name = customerinfoEntity.getCustomername();
		String gender = customerinfoEntity.getGender();
		String tel = customerinfoEntity.getTel();
		String UID = customerinfoEntity.getUid();
		String address = customerinfoEntity.getAddress();
		String customerWork = customerinfoEntity.getCustomerwork();
		String customerType = customerinfoEntity.getCustomertype();
		String discredit = customerinfoEntity.getDiscredit();
		String litigation = customerinfoEntity.getLitigation();
		int monthlyIncome = customerinfoEntity.getMonthlyincome();
		int bankId = customerinfoEntity.getBankid();
		int goodsId = customerinfoEntity.getGoodsid();
		String customerState = customerinfoEntity.getCustomerstate();

		customerService.addList(name, gender, tel, UID, address, customerWork, customerType, discredit, litigation,
				monthlyIncome, bankId, goodsId, customerState);

		return SUCCESS;
	}

	public String find() {
		int id = customerinfoEntity.getId();
		String name = customerinfoEntity.getCustomername();
		String customerType = customerinfoEntity.getCustomertype();
		String UID = customerinfoEntity.getUid();

		HttpServletRequest request = ServletActionContext.getRequest();

		String page = request.getParameter("pageNo");// ��ǰҳ��
		String rows = request.getParameter("pageSize");// ��ǰ����

		List<CustomerInfoVO> list = customerService.findList(id, name, customerType, UID,
				(Integer.parseInt(page) - 1) * Integer.parseInt(rows), Integer.parseInt(rows));
		List<CustomerinfoEntity> list1 = customerService.findCustomer();

		int total = list1.size();// ������

		// ��ҳ��
		int pages = total % Integer.parseInt(rows) == 0 ? total / Integer.parseInt(rows)
				: total / Integer.parseInt(rows) + 1;

		info.put("list", list);
		info.put("pageNum", page);// ��ǰҳ��
		info.put("pages", pages);// ��ҳ��
		info.put("total", total);// ������

		return SUCCESS;
	}

}
